package com.sm42.goodsService.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sm42.baseService.impl.BaseCrmpServiceImpl;
import com.sm42.goodsEntity.Goods;
import com.sm42.goodsService.GoodsService;
import com.sm42.mapper.goodsMapper.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class GoodsServiceImpl extends BaseCrmpServiceImpl<Goods> implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public List<Goods> findAllList() {
        Page<Goods> page = new Page<>(1, 2);
        Page<Goods> goodsPage = goodsMapper.selectPage(page, null);
        System.out.println(goodsPage.getTotal());
        return goodsPage.getRecords();
    }

//    @Override
//    public List<Goods> findGoodsList(Goods goods) {
//        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
//        wrapper.eq("good_name",goods.getGoodName()).or()
//                .eq("good_model",goods.getGoodModel()).or()
//                .eq("good_color",goods.getGoodColor()).or()
//                .eq("good_class",goods.getGoodClass()).or()
//                .eq("good_brand",goods.getGoodBrand());
//        List<Goods> goodsList = goodsMapper.selectList(wrapper);
//        return goodsList;
//    }

    @Override
    public List<Goods> findSearch(Goods goods) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.eq(true, "good_status", 0)
                .eq((goods.getGoodBrand() != 0), "good_brand", goods.getGoodBrand())
                .eq((goods.getGoodId() != 0), "good_id", goods.getGoodId())
                .like(ObjectUtils.isNotNull(goods.getGoodName()), "good_name", goods.getGoodName())
                .like(ObjectUtils.isNotNull(goods.getGoodModel()), "good_model", goods.getGoodModel())
                .like(ObjectUtils.isNotNull(goods.getGoodColor()), "good_color", goods.getGoodColor())
                .like((goods.getGoodClass() != 0), "good_class", goods.getGoodClass());
        List<Goods> goodsList = goodsMapper.selectList(wrapper);
        return goodsList;
    }

    @Override
    public int addGoods(Goods goods) {
        goods.setGoodStatus(0);
        goods.setCreateTime(LocalDateTime.now());
        return add(goods);
    }

    @Override
    public int updateGoods(Goods goods) {
        goods.setUpdateTime(LocalDateTime.now());
        System.out.println(goods);
        return update(goods);
    }

    @Override
    public int deleteGoods(Long id) {
        return deleteById(id);
    }

//    @Override
//    public Goods findGoodsById(Long id) {
//        return null;
//    }
}
